package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.DorisStorage;
import com.wande.dataplatform.domain.vo.*;
import com.wande.dataplatform.service.IDorisStorageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Doris存储管理Controller
 *
 * @author dataplatform
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/doris-storage")
@Tag(name = "Doris存储管理", description = "Doris存储管理接口")
public class DorisStorageController extends BaseController {

    private final IDorisStorageService dorisStorageService;

    /**
     * 查询Doris存储元数据列表
     */
    @SaCheckPermission("dataplatform:doris-storage:list")
    @GetMapping("/list")
    @Operation(summary = "查询Doris存储元数据列表")
    public TableDataInfo<DorisStorageVO> list(DorisStorage bo, PageQuery pageQuery) {
        return dorisStorageService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取Doris存储元数据详细信息
     */
    @SaCheckPermission("dataplatform:doris-storage:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取Doris存储元数据详细信息")
    public R<DorisStorageVO> getInfo(@Parameter(description = "主键", required = true)
                                      @PathVariable Long id) {
        return R.ok(dorisStorageService.queryById(id));
    }

    /**
     * 同步Doris元数据
     */
    @SaCheckPermission("dataplatform:doris-storage:sync")
    @Log(title = "Doris存储管理", businessType = BusinessType.UPDATE)
    @PostMapping("/sync")
    @Operation(summary = "同步Doris元数据")
    public R<Void> syncMetadata() {
        dorisStorageService.syncDorisMetadata();
        return R.ok();
    }

    /**
     * 获取数据库树形结构
     */
    @SaCheckPermission("dataplatform:doris-storage:tree")
    @GetMapping("/tree")
    @Operation(summary = "获取数据库树形结构")
    public R<List<DorisDatabaseTreeVO>> getDatabaseTree() {
        return R.ok(dorisStorageService.getDatabaseTree());
    }

    /**
     * 查询表结构
     */
    @SaCheckPermission("dataplatform:doris-storage:structure")
    @GetMapping("/structure")
    @Operation(summary = "查询表结构")
    public R<List<DorisTableStructureVO>> getTableStructure(
            @Parameter(description = "数据库名称", required = true) @RequestParam String databaseName,
            @Parameter(description = "表名称", required = true) @RequestParam String tableName) {
        return R.ok(dorisStorageService.getTableStructure(databaseName, tableName));
    }

    /**
     * 查询表统计信息
     */
    @SaCheckPermission("dataplatform:doris-storage:statistics")
    @GetMapping("/statistics")
    @Operation(summary = "查询表统计信息")
    public R<DorisTableStatisticsVO> getTableStatistics(
            @Parameter(description = "数据库名称", required = true) @RequestParam String databaseName,
            @Parameter(description = "表名称", required = true) @RequestParam String tableName) {
        return R.ok(dorisStorageService.getTableStatistics(databaseName, tableName));
    }

    /**
     * 预览表数据
     */
    @SaCheckPermission("dataplatform:doris-storage:preview")
    @GetMapping("/preview")
    @Operation(summary = "预览表数据")
    public R<DorisDataPreviewVO> previewTableData(
            @Parameter(description = "数据库名称", required = true) @RequestParam String databaseName,
            @Parameter(description = "表名称", required = true) @RequestParam String tableName,
            @Parameter(description = "页码") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return R.ok(dorisStorageService.previewTableData(databaseName, tableName, pageNum, pageSize));
    }

    /**
     * 查询分区列表
     */
    @SaCheckPermission("dataplatform:doris-storage:partition")
    @GetMapping("/partition/list/{storageId}")
    @Operation(summary = "查询分区列表")
    public R<List<DorisPartitionVO>> getPartitionList(
            @Parameter(description = "存储ID", required = true) @PathVariable Long storageId) {
        return R.ok(dorisStorageService.getPartitionList(storageId));
    }

    /**
     * 删除分区
     */
    @SaCheckPermission("dataplatform:doris-storage:partition:delete")
    @Log(title = "Doris存储管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/partition")
    @Operation(summary = "删除分区")
    public R<Void> deletePartition(
            @Parameter(description = "数据库名称", required = true) @RequestParam String databaseName,
            @Parameter(description = "表名称", required = true) @RequestParam String tableName,
            @Parameter(description = "分区名称", required = true) @RequestParam String partitionName) {
        dorisStorageService.deletePartition(databaseName, tableName, partitionName);
        return R.ok();
    }
}
